package com.jf3q.common.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jf3q.common.domain.EduVideo;
import com.jf3q.common.service.VideoService;
import com.jf3q.common.vo.ResultVo;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.jf3q.common.service.UploadVideoService;

import java.io.IOException;


@RestController
@Tag(name = "阿里云文件")
public class UploadController {

    @Autowired
    UploadVideoService uploadVideoServer;

    @Autowired
    VideoService videoService;
    @PostMapping("/upload/{type}")
    @Operation(summary = "阿里云oss文件上传")
    public ResultVo upload(MultipartFile file, @PathVariable String type) {
        String uploadInfo = uploadVideoServer.upload(file, type);
        return ResultVo.success("上传成功", uploadInfo);
    }

    @DeleteMapping("/delOss")
    @Operation(summary = "阿里云oss文件删除")
    public ResultVo delFile(String fileName) {
        /*System.out.println("进入删除文件方法");
        int index =0;
        if (fileName.contains("aliyuncs.com/")){
           index= fileName.indexOf("aliyuncs.com/")+13;
        }

        String name = fileName.substring(index);*/
        uploadVideoServer.delOssFile(fileName);
        return ResultVo.success(fileName + "文件删除成功", null);
    }

    @Operation(summary = "阿里云视频上传")
    @PostMapping("/video")
    public ResultVo uploadVide(MultipartFile file) throws IOException {
        return ResultVo.success("视频上传成功", uploadVideoServer.uploadVide(file));
    }

    @Operation(summary = "获取阿里云视频地址")
    @GetMapping("/getPlayUrl/{vid}")
    public ResultVo getPlayUrl(@PathVariable String vid) {
        EduVideo video = videoService.getById(vid);
        LambdaQueryWrapper<EduVideo> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(EduVideo::getId,vid);
        video.setPlayCount(video.getPlayCount() + 1);
        videoService.updateById(video);
        return ResultVo.success("播放地址", uploadVideoServer.getPlayUrl(vid));
    }

    @Operation(summary = "获取阿里云视频凭证")
    @GetMapping("/gerPlayAuth/{vid}")
    public ResultVo getPlayAuth(@PathVariable String vid) {
        return ResultVo.success("播放凭证", uploadVideoServer.getPlayAuth(vid));
    }

    @Operation(summary = "删除阿里云视频")
    @DeleteMapping("/del/{vid}")
    public ResultVo del(@PathVariable String vid) {
        return ResultVo.success(uploadVideoServer.delVideo(vid), null);
    }

}
